package com.itheima.entry.controller.impl;

import com.itheima.entry.controller.IStudentController;
import com.itheima.entry.domain.Student;
import com.itheima.entry.factory.StudentFactroy;
import com.itheima.entry.service.IStudentService;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;


public class StudentControllerImpl implements IStudentController {
    private Scanner sc = new Scanner(System.in);
    public static StatisticsControllerImpl statisticsController = new StatisticsControllerImpl();
    public static IStudentService service = StudentFactroy.getService();

    @Override
    public void star() {
        Loop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.所有学生 5.查询学生 6.统计数据 7. 返回上一层 ");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    // System.out.println("添加成功");
                    add();
                    break;
                case "2":
                    // System.out.println("删除成功");
                    remove();
                    break;
                case "3":
                    // System.out.println("修改成功");
                    update();
                    break;
                case "4":
                    // System.out.println("查询成功");
                    allStudents();
                    break;
                case "5":
                    // System.out.println("查询成功");
                    findStudents();
                    break;
                case "6":
                    statisticsController.menu();
                    break;
                case "7":
//                    System.out.println("感谢您使用学生管理系统, 再见!");
                    return;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }

    private void findStudents() {

        while (true) {
            System.out.println("查询学生:1.名字搜索 2.手机号搜索 3. 返回上一层");
            switch (sc.next()) {
                case "1":
                    //System.out.println("名字搜索结果");
                    sameNameStudents();
                    break;
                case "2":
                    //System.out.println("手机号搜索结果");
                    phoneToStudent();
                    break;
                case "3":
                    //返回上一层
                    return;
                default:
                    System.out.println("输入有误");
                    break;


            }
        }


    }

    @Override
    public void add() {
        String sid = null;
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            boolean exists = service.isExists(sid);
            if (exists) {
                System.out.println("该学号已被占用,请重新输入");
            } else {
                break;
            }
        }
        Student student = getStudentInfo(sid);
        service.add(student);
        System.out.println("添加成功!");
    }

    private Student getStudentInfo(String sid) {
        Student student = new Student();
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入性别:");
        String gender = sc.next();
        LocalDate birthday = null;
        while (true) {
            System.out.println("请输入生日:(格式例如:2000/08/22)");
            String bir = sc.next();
            try {
                DateTimeFormatter date = DateTimeFormatter.ofPattern("yyyy/MM/dd");
                birthday = LocalDate.parse(bir, date);
                break;
            } catch (Exception e) {
                System.out.println("请输入正确的格式!");
            }
        }
        System.out.println("请输入手机号:");
        String phoneNum = sc.next();
        System.out.println("请输入加入的组号:");
        int group = sc.nextInt();
        student.setId(sid);
        student.setName(name);
        student.setGender(gender);
        student.setBirthday(birthday);
        student.setPhoneNum(phoneNum);
        student.setGroup(group);
        return student;
    }

    @Override
    public void remove() {
        String id;
        while (true) {
            System.out.println("请输入要删除的学号");
            id = sc.next();
            boolean exists = service.isExists(id);
            if (!exists) {
                System.out.println("当前学号信息不存在");
            } else {
                break;
            }

        }
        service.remove(id);
        System.out.println("删除成功！");
    }


    @Override
    public void update() {
        String sid = null;
        while (true) {
            System.out.println("请输入修改的学号");
            sid = sc.next();
            boolean exists = service.isExists(sid);
            if (!exists) {
                System.out.println("该学号不存在,请重新输入");
            } else {
                break;
            }

        }

        Student student = getStudentInfo(sid);
        service.update(student);
        System.out.println("修改成功!");
    }


    @Override
    public void allStudents() {
        ArrayList<Student> list = service.allStudents();
        if (list == null || list.size() == 0) {
            System.out.println("暂无学生数据");
            return;
        }
        showList(list);
    }

    //打印集合
    private void showList(ArrayList<Student> list) {
        System.out.println("学号\t\t姓名\t\t性别\t年龄\t\t生日\t\t\t手机号码\t\t\t组名");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student != null) {
                System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getGender() + "\t\t" + student.getAge() + "\t\t" + student.getBirthday() + "\t\t" + student.getPhoneNum() + "\t\t" + student.getGroup());
            }
        }
    }

    @Override
    public void sameNameStudents() {

        System.out.println("请输入姓名:");
        ArrayList<Student> list = service.sameNameStudents(sc.next());
        if (list == null || list.size() == 0) {
            System.out.println("暂无学生数据");
            return;
        }
        showList(list);
    }

    @Override
    public void phoneToStudent() {
        System.out.println("请输入手机号:");
        ArrayList<Student> list = service.phoneToStudent(sc.next());
        if (list == null || list.size() == 0) {
            System.out.println("暂无学生数据");
            return;
        }
        showList(list);

    }

    @Override
    public void statistics() {
        statisticsController.menu();
    }

}
